/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.teamservice.service;

import java.math.BigDecimal;
import java.util.List;

import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.rewardparameters.entity.RewardParameters;
import com.jeeplus.modules.rewardparameters.mapper.RewardParametersMapper;
import com.jeeplus.modules.rewardparameters.service.RewardParametersService;
import com.jeeplus.modules.saleorder.service.SaleOrderService;
import com.jeeplus.modules.subrecord.mapper.SubRecordMapper;
import com.jeeplus.modules.traderecord.mapper.TradeRecordMapper;
import com.jeeplus.modules.traderecord.service.TradeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.teamservice.entity.TeamService;
import com.jeeplus.modules.teamservice.mapper.TeamServiceMapper;

/**
 * 团队手续费Service
 * @author w
 * @version 2020-12-03
 */
@Service
@Transactional(readOnly = true)
public class TeamServiceService extends CrudService<TeamServiceMapper, TeamService> {

	@Autowired
	private AccountService accountService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private RewardParametersService rewardParametersService;
	@Autowired
	private RewardParametersMapper rewardParametersMapper;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private TradeRecordMapper tradeRecordMapper;
	@Autowired
	private SubRecordMapper subRecordMapper;

	public TeamService get(String id) {
		return super.get(id);
	}
	
	public List<TeamService> findList(TeamService teamService) {
		return super.findList(teamService);
	}
	
	public Page<TeamService> findPage(Page<TeamService> page, TeamService teamService) {
		return super.findPage(page, teamService);
	}
	
	@Transactional(readOnly = false)
	public void save(TeamService teamService) {
		super.save(teamService);
	}
	
	@Transactional(readOnly = false)
	public void delete(TeamService teamService) {
		super.delete(teamService);
	}
	public Double getTodayStatic() {
		Double money = mapper.getTodayStatic();
		return money;
	}


	/**
	 * 处理团队手续费收益
	 * @param member
	 * @param charge
	 */
	@Transactional(readOnly = false)
	public void manageTeamService(Member member, double charge, String orderNo) {
		//给上级添加手续费收益
		int flag1 = 0;
		int flag2 = 0;
		int flag3 = 0;
		String recordName = "";
		Double sendMoney = 0.0;
		Double levelMoney = 0.0;
		boolean isSend = false;
		//1星手续费收益
		Double percentOne = 0.0;
		//2星手续费收益
		Double percentTwo = 0.0;
		//3星手续费收益
		Double percentThree = 0.0;
		//3星手续费平级收益
		Double percentSameThree = 0.0;
		Integer count2 = 0;
		Integer count3 = 0;
		//平台总手续费
//		Double sumCharge = tradeRecordMapper.findAllTotal() * 0.1;
		//平台总业绩
//		Double sumTeam = tradeRecordMapper.findAllTotal() * 0.1 + subRecordMapper.findAllSubMoney();
		if (member.getP() != null){
			Member pMember = memberService.get(member.getP().getId());
			if (pMember != null){
				//查询奖励比例
				List<RewardParameters> rewardParametersList = rewardParametersMapper.findList(new RewardParameters());
				if (rewardParametersList.size()>0){
					percentOne = rewardParametersList.get(0).getIntePercentOne();
					percentTwo = rewardParametersList.get(0).getIntePercentTwo();
					percentThree = rewardParametersList.get(0).getIntePercentThree();
					percentSameThree = rewardParametersList.get(0).getInteLevelThree();
					levelMoney = charge*percentOne;
				}
				while (pMember != null){
					boolean  isEffective = memberService.isOrNotEffective(pMember);

					Account account = accountService.get(pMember.getAccount().getId());

					if (("1".equals(pMember.getUsertype()) || "0".equals(pMember.getUsertype())) && isEffective){
						//只享受一代会员的收益
						if (flag1 == 0 && flag2 == 0 && flag3 == 0 && member.getP().getId().equals(pMember.getId())){
							sendMoney = charge*percentOne;
							levelMoney = 0.0;
							account.setYue(account.getYue()+sendMoney);
							account.setTodayTeam(account.getTodayTeam()+sendMoney);
							account.setTeamProfit(account.getTeamProfit()+sendMoney);
							account.setTotalProfit(account.getTotalProfit()+sendMoney);
							account.setTodayProfit(account.getTodayProfit()+sendMoney);
							recordName = "1星奖励";
							flag1 = 1;
							isSend = true;
						}else {
							isSend = false;
						}
					}else if ("2".equals(pMember.getUsertype()) && isEffective){
						if (flag2 == 0){
							count2 = memberService.findLevelCount(pMember);
							BigDecimal m = BigDecimal.valueOf(charge*percentTwo).divide(BigDecimal.valueOf(count2+1),2,BigDecimal.ROUND_HALF_DOWN);
//							levelMoney = charge*percentOne;
							sendMoney = levelMoney + m.doubleValue();
							levelMoney = 0.0;
							account.setYue(account.getYue()+sendMoney);
							account.setTodayTeam(account.getTodayTeam()+sendMoney);
							account.setTeamProfit(account.getTeamProfit()+sendMoney);
							account.setTotalProfit(account.getTotalProfit()+sendMoney);
							account.setTodayProfit(account.getTodayProfit()+sendMoney);
							recordName = "2星奖励";
							flag2 = 1;
							isSend = true;
						}else if (flag2 == 1){
							BigDecimal m = BigDecimal.valueOf(charge*percentTwo).divide(BigDecimal.valueOf(count2+1),2,BigDecimal.ROUND_HALF_DOWN);
//							levelMoney = charge*percentOne;
							sendMoney = levelMoney + m.doubleValue();
							levelMoney = 0.0;
							account.setYue(account.getYue()+sendMoney);
							account.setTodayTeam(account.getTodayTeam()+sendMoney);
							account.setTeamProfit(account.getTeamProfit()+sendMoney);
							account.setTotalProfit(account.getTotalProfit()+sendMoney);
							account.setTodayProfit(account.getTodayProfit()+sendMoney);
							recordName = "2星奖励";
//							flag2 = 2;
							isSend = true;
						} else {
							isSend = false;
						}

					}else if ("3".equals(pMember.getUsertype()) && isEffective){
						if (flag3 == 0){
							count3 = memberService.findLevelCount(pMember);
							BigDecimal m = BigDecimal.valueOf(charge*percentThree).divide(BigDecimal.valueOf(count3+1),2,BigDecimal.ROUND_HALF_DOWN);
//							levelMoney = charge*percentOne;
							sendMoney = levelMoney + m.doubleValue();
							levelMoney = 0.0;
							account.setYue(account.getYue()+sendMoney);
							account.setTodayTeam(account.getTodayTeam()+sendMoney);
							account.setTeamProfit(account.getTeamProfit()+sendMoney);
							account.setTotalProfit(account.getTotalProfit()+sendMoney);
							account.setTodayProfit(account.getTodayProfit()+sendMoney);
							recordName = "3星奖励";
							flag3 = 1;
							isSend = true;
						} else if (flag3 == 1){
							BigDecimal m = BigDecimal.valueOf(charge*percentThree).divide(BigDecimal.valueOf(count3+1),2,BigDecimal.ROUND_HALF_DOWN);
							sendMoney = levelMoney + m.doubleValue();
							account.setYue(account.getYue()+sendMoney);
							account.setTodayTeam(account.getTodayTeam()+sendMoney);
							account.setTeamProfit(account.getTeamProfit()+sendMoney);
							account.setTotalProfit(account.getTotalProfit()+sendMoney);
							account.setTodayProfit(account.getTodayProfit()+sendMoney);
							recordName = "3星奖励";
//							flag3 = 2;
							isSend = true;
						} else {
							isSend = false;
						}
					}else {
						isSend = false;
					}
					if (isSend){
						TeamService teamService = new TeamService();
						teamService.setMember(pMember);
						teamService.setMoney(sendMoney);
						teamService.setNumber(member.getLoginName());
						teamService.setGoodsname(recordName);
						save(teamService);
//						accountService.save(account);
						accountMapper.saveTeamAndTeamProfit(0.0,sendMoney,account.getId());
					}
					if (pMember.getP() != null){
						pMember = memberService.get(pMember.getP().getId());
					}else {
						break;
					}
				}
			}
		}
	}

	public List<TeamService> findListByDays(TeamService teamService) {
		return mapper.findListByDays(teamService);
	}
}